﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using INTEX.DataBase;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class ITXABPLAN : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        DataSet setDISPO;
        double FLeng = 0, GLeng = 0, WLeng = 0;
        int FLeadTime = 0, GLeadTime = 0, WLeadTime = 0, YLeadTime = 14;
        double Fnum = 0, Gnum = 0, Wnum = 0;
        string FEnd = "", GEnd = "", WEnd = "", YEnd = "";
        string DISPONUM = "";
        DataRow lastrow = null; 
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        
        ITXABPLANData m_Data, m_DataX;
        ITXABPLANReport m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        string UserID = "";
        string WTYPE
        {
            get
            {
                if (cbWTYE.Text == "生产单")
                    return "V";
                if (cbWTYE.Text == "销售单")
                    return "K";
                return "V";                
            }
        }
        CollectionViewSource vwWASTAMM
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwWASTAMM");
            }
        }
        DataRow dwWASTAMM
        {
            get
            {
                if (vwWASTAMM.View == null)
                    return null;
                if ((DataRowView)vwWASTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        string KRS
        {
            get
            {
                return m_Data.Left(txtVKPAUFKRS.Text.Trim() + "  ", 2);
            }
            set
            {
                txtVKPAUFKRS.Text = m_Data.Left(txtVKPAUFKRS.Text.Trim() + value, 2);
            }
        }
        string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFLFD.Text.Trim(), 6);
            }
            set
            {
                txtVKPAUFLFD.Text = m_Data.Right("000000" + value, 6); ;
            }
        }
        string POS
        {
            get
            {
                return m_Data.Right("000" + txtVKPAUFPOS.Text.Trim(), 3);
            }
            set
            {
                txtVKPAUFPOS.Text = m_Data.Right("000" + value, 3);
            }
        } 
        
        public ITXABPLAN()
        {
            InitializeComponent();
            this.Name = "生产订单管理";
            this.Title = "生产订单管理";
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
        }
    
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                m_Data = new ITXABPLANData(OracleDB, proxy);
                txtUserID.Text = (string)dicParamater["User"];
                txtPassword.Password = (string)dicParamater["Pwd"];
                m_Data.UserID = Helpers.Helper.Left((string)dicParamater["User"] + "           ", 8);
                KRS = "J3";
                LFD = "002006";
                POS = "001";
                cbWTYE.Text = "生产单";
                ReloadCon();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count > 0)
            {
                DataRow dwUser = setResult.Tables[0].Rows[0];
                txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
                UserID = userID;
                m_Data.UserID = userID;
                this.UserID = userID;

                m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                    + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
                m_Data.MANDANT = Mandant;
                m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "已登录";
                txtLogIn.Background = new SolidColorBrush(Colors.Green);

            }
        }

        #endregion 

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.Clear();
                DataSet SResult = new DataSet();
                DataRow dwData = SetData.VKPOS.NewRow();
                dwData["VKPMANDANT"] = "00";
                dwData["VKPAUFKRS"] = KRS;
                dwData["VKPAUFLFD"] = LFD;
                SResult = m_Data.LoadData(dwData);
                foreach (DataTable tb in SResult.Tables)
                {
                    string tbname = tb.TableName;
                    SetData.Merge(tb);
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ReloadCon()
        {
            DataSet SResult = null;
            SResult = m_Data.LoadEnv();
            SetEnv.Merge(SResult);
        }
        private void btnAddCompare_Click(object sender, RoutedEventArgs e)
        {
            
            DataSet SResult = new DataSet();
            DataRow dwData = SetData.VKPOS.NewRow();
            dwData["VKPMANDANT"] = "00";
            dwData["VKPAUFKRS"] = KRS;
            dwData["VKPAUFLFD"] = LFD;
            dwData["VKPAUFPOS"] = POS;
            SResult = m_Data.LoadData(dwData, WTYPE);
            foreach (DataTable tb in SResult.Tables)
            {
                string tbname = tb.TableName;
                SetData.Merge(tb);
            }
        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            SetData.Clear();
        }
        private void btnDeleteABON_Click(object sender, RoutedEventArgs e)
        {
            //m_Data.DeleteABON(KRS, LFD, POS);
            m_Data.DeleteAV95(KRS, LFD, POS); 
        }
        private void btnDeleteAB_Click(object sender, RoutedEventArgs e)
        { 
            m_Data.Delete("00", KRS, LFD, POS);
        }
        private void txtVKPAUFLFD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {

            DataSet setRpt = new DataSet();

            if (setRpt == null)
                return;
            m_DataX = new ITXABPLANData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            if (m_DataX.exAgent._app == null)
            {
                m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
            }
            m_rpt = new ITXABPLANReport(dicParamater);
            m_rpt.dsSource = SetData;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }
    }
}
